<?
include_once 'include/processes.php';
$Login_Process = new Login_Process;
$Login_Process->check_status($_SERVER['SCRIPT_NAME']);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Crash Report</title>
<link href="include/style.css" rel="stylesheet" type="text/css">
<body>

<div class="center">
<?php 

function checkDump() {
	if (empty($_FILES['dump_file'])) {
		echo "<p>Error: No dump file!</p>";
		return false;
	}	
	if ($_FILES['dump_file']['error'] != 0) {
		echo "<p>Error: Dump file corrupted!</p>";
		return false;
	}
	return true;
}

function saveDump($dumpFilePath) {
	//echo "<p>Saving your dump file to ".$dumpFilePath."</p>";

	if (file_exists($dumpFilePath)) {
		echo "<p>Error: Duplicate dump file!</p>";
		return false;
	}

	// Save the file.
	if (!move_uploaded_file($_FILES['dump_file']['tmp_name'], $dumpFilePath)) {
		echo "<p>Error: Failed to save the dump file!</p>";
		return false;
	}
	
	return true;
}

function storeExceptionAddress($crashFilePath) 
{
    $fileHandle = fopen($crashFilePath, "r"); 
    if (!$fileHandle) {
        error_log("can't open the crash file - %s", $crashFilePath);
        return -1;
    }

	$addrStr = "";
    while (!feof($fileHandle)) {
        $buffer = fgets($fileHandle, 4096); // read a line  
        //echo "<p>buffer".$buffer."</p>";
		if (preg_match("/Exception Addr/i", $buffer)) {
			$pos = strpos($buffer, ": ");
			$addrStr = substr($buffer, $pos + 9);
			$addrStr = trim($addrStr);
        	echo "<p>address string[".$addrStr."]</p>";
			break;
		}	
    }

	$lp = new Login_Process;
	$cid = $lp->Update_Exception_Address($addrStr);
	if ($cid == -1) {
		echo "<p>Failed to store exception address.</p>";
	}

	return $cid;	
}


function updateDbWithCrashFile($dumpFilePath, $procCode)
{
	$crashFilePath = CRASH_ROOT.basename($_FILES['dump_file']['name']);
	$crashFilePath = preg_replace('"\.dmp$"', '.crash', $crashFilePath);

	echo "<p>Saving crash file to ".$crashFilePath."</p>";
	echo "<p>Saving dump file to ".$dumpFilePath."</p>";

	if (!file_exists($crashFilePath)) {
		if (!move_uploaded_file($_FILES['crash_file']['tmp_name'], $crashFilePath)) {
			echo "<p>Error: Failed to save the crash file!</p>";
			return;
		}	

		$cid = storeExceptionAddress($crashFilePath);
	} else {
		echo "<p>Error: Duplicate crash file!</p>";
		return;
	}
	
	$lp = new Login_Process;
	$lp->Update_Db_With_Crash_File($dumpFilePath, $procCode, $crashFilePath, $cid);
}

function updateDbWithoutCrashFile($dumpFilePath, $procCode)
{
	$lp = new Login_Process;
	return $lp->Update_Db_Without_Crash_File($dumpFilePath, $procCode);
}

function getProcessNameCode()
{
	$processName = strval($_POST['process_name']);
	echo "<p>Process name : ".$processName."</p>";
	if (strcasecmp($processName, "ArcheAge.exe") == 0) {
		return 1;
	} else if (strcasecmp($processName, "world_server.exe") == 0) {
		return 2;
	} else if (strcasecmp($processName, "db_server.exe") == 0) {
		return 3;
	} else if (strcasecmp($processName, "zone_server.exe") == 0) {
		return 4;
	}
	return 0;
}

function updateDb($dumpFilePath) {	
	// First see if the crash file is present
	$crashFileAvailable = false;	
	if (!empty($_FILES['crash_file']) && $_FILES['crash_file']['error'] == 0) {
		$crashFileAvailable = true;
	} else {
		echo "<p>Warn: No crash file.</p>";	
	}

	$processNameCode = getProcessNameCode();

	// TODO : Analyze the log file and extract addition dump-related data.
	if ($crashFileAvailable) {
		updateDbWithCrashFile($dumpFilePath, $processNameCode);
	} else {
		if (updateDbWithoutCrashFile($dumpFilePath, $processNameCode)) {
			echo "<p>Dump upload successful!!</p>";	
		} else {
			echo "<p>Error: DB update failed.</p>";	
		}
	}
}

if (checkDump()) {
	$dumpFileName = basename($_FILES['dump_file']['name']);
	$dumpFileName = DUMP_ROOT.$dumpFileName;
	if (saveDump($dumpFileName)) {
		updateDb($dumpFileName);
	}
}

?>
</div>

<div class="center">
<a href="main.php">Back to main</a> | <a href="view_dumps.php">View Dumps</a>
</div>

</body>
</html>

